$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require "rupeas"




Rupeas.new do

Type :transmit do
 with :name =>:destination, :fieldtype=> :integer#, :range => [1,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010,2011,2012], :notification => :error
 with :name =>:origin, :fieldtype => :integer#, :range => 2000..2012,:notification => :error
 with :name =>:seqNo, :fieldtype => :integer#, :range => 0..65535, :notification => :error
 with :name =>:nodeid,:fieldtype=> :integer#, :range => 2000..2012,:notification => :error
end

Type :receive do
 with :name =>:origin, :fieldtype => :integer#, :range => 2000..2012,:notification => :error
 with :name =>:seqNo, :fieldtype => :integer#, :range => 0..65535, :notification => :error
 with :name =>:nodeid,:fieldtype=> :integer#, :range => 2000..2012,:notification => :error
end

all = loadlog('roman-trace.txt')
p all.length

transmissions = all[:type=>:transmit]

p transmissions.length

routestart = transmissions.transform do |sending|
  constraint sending[:origin] == sending[:nodeid]       # partition
  merging :type => :route,:nodeid => sending[:nodeid],:seqNo => sending[:seqNo],:origin => sending[:origin]
end


forward_pairs = transmissions.transform do |sending, forwarding|
  constraint sending[:origin] == forwarding[:origin]        # partition (1)
  constraint sending[:seqNo] == forwarding[:seqNo]             # partition (2)
  select sending[:destination] == forwarding[:nodeid]
  merging :type => :forward, :nodeid => forwarding[:nodeid], :seqNo => sending[:seqNo], :origin => sending[:origin]
end

ratio = Hash.new
success = Hash.new
routestart_node = Hash.new
destined_to = Hash.new

p all.values(:nodeid)

p forward_pairs.length

all.values(:nodeid).each do |node_id|
    success[node_id] = forward_pairs[:type=>:forward][:nodeid=>node_id].length
    routestart_node[node_id] = routestart[:nodeid=>node_id].length
    destined_to[node_id] = transmissions[:destination=>node_id].length
    ratio[node_id] = success[node_id].to_f / destined_to[node_id].to_f if destined_to[node_id] > 0
end

puts "per node statistics"

destined_to.each do |key, value|
  puts "node is #{key}"
  puts "success from forwarding is #{success[key]}"
  puts "total addressed is #{destined_to[key]}"
  puts "ratio is #{ratio[key]}"
  puts "started routes from this node is #{routestart_node[key]}"
end

end
